package cn.jinjimi.app.custom.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.event.CustomerListChangeEvent;
import cn.jinjimi.app.custom.ui.adapter.CustomSearchAdapter;
import cn.jinjimi.app.search.CustomSearchRecord;
import cn.jinjimi.app.search.Record;
import cn.jinjimi.app.search.SearchProxy;
import cn.jinjimi.app.search.SearchResultListener;
import cn.jinjimi.app.main.extend.BaseActivity;
import cn.jinjimi.app.main.widget.XListView.XListView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Roy
 * Date: 15/12/4
 */
public class CustomSearchActivity extends BaseActivity implements View.OnClickListener,
        SearchResultListener, AdapterView.OnItemClickListener {

    private EditText edSearch;
    private TextView tvNoResult;
    private XListView mListView;
    private CustomSearchAdapter mAdapter;

    private SearchProxy searchProxy;
    private boolean isReturn;
    private List<Custom> targetCustoms;

    public static void launch(Activity startActivity, boolean isReturn, int quesCode) {
        launch(startActivity, null, isReturn, quesCode);
    }

    public static void launch(Activity startActivity, List<Custom> targetCustoms, int quesCode) {
        launch(startActivity, targetCustoms, true, quesCode);
    }

    public static void launch(Activity startActivity, List<Custom> targetCustoms, boolean isReturn, int quesCode) {
        if (startActivity == null)
            return;
        Intent intent = new Intent(startActivity, CustomSearchActivity.class);
        if (targetCustoms != null)
            intent.putExtra(BundleKey.PARAM_CUSTOMS, (Serializable) targetCustoms);
        intent.putExtra(BundleKey.PARAM_CUSTOM_RETURN, isReturn);
        startActivity.startActivityForResult(intent, quesCode);
        startActivity.overridePendingTransition(0, 0);
    }

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_custom_search);
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        edSearch = (EditText) findViewById(R.id.edit_search);
        tvNoResult = (TextView) findViewById(R.id.tv_noresult);

        mListView = (XListView) findViewById(R.id.listview);
        mListView.setPullLoadEnable(false);
        mListView.setPullRefreshEnable(false);
        mListView.setTransparentBg();
        mAdapter = new CustomSearchAdapter(this);
        mListView.setAdapter(mAdapter);
    }

    protected void initListener() {
        mListView.setOnItemClickListener(this);
        findViewById(R.id.tv_cancle).setOnClickListener(this);
        edSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                searchProxy.query(s.toString());
            }
        });
    }

    private void initData() {
        if (getIntent().hasExtra(BundleKey.PARAM_CUSTOMS)) {
            targetCustoms = (List<Custom>) getIntent().getSerializableExtra(BundleKey.PARAM_CUSTOMS);
        }
        isReturn = getIntent().getBooleanExtra(BundleKey.PARAM_CUSTOM_RETURN, true);
        searchProxy = new SearchProxy(this, targetCustoms);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancle:
                finish();
                break;
        }
    }

    @Override
    public void onQueryStart() {

    }

    @Override
    public void onQueryCanceled() {

    }

    @Override
    public void onQueryResultGened(List<Record> records, boolean hasNextPage) {
        mAdapter.setItems(records);
        if (StringUtils.isNotEmpty(edSearch.getText().toString()))
            tvNoResult.setVisibility(records == null || records.size() == 0 ? View.VISIBLE : View.GONE);
        else
            tvNoResult.setVisibility(View.GONE);
    }

    @Override
    public void onQueryFinish() {

    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CustomSearchRecord customSearchRecord = (CustomSearchRecord) mAdapter.getItem(position - mListView.getHeaderViewsCount());
        if (isReturn) {
            selecterCustom(customSearchRecord.custom);
        } else {
            openCustom(customSearchRecord.custom);
        }
    }

    /* 直接打开客户档案页 */
    private void openCustom(Custom custom) {
        CustomArchivesActivity.launch(this, custom);
    }

    /* 成功选择客户并返回 */
    private void selecterCustom(Custom custom) {
        Intent intent = new Intent();
        intent.putExtra(BundleKey.PARAM_CUSTOM, custom);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomerListChangeEvent(CustomerListChangeEvent event) {
        searchProxy.query(edSearch.getText().toString().trim());
    }
}
